package com.baiduspring.utils.elasticsearch;

import com.baiduspring.function.elasticsearch.ElasticsearchHandle;
import com.baiduspring.function.elasticsearch.operation.Doc;
import com.baiduspring.function.elasticsearch.operation.QueryInput;
import com.baiduspring.function.elasticsearch.operation.QueryResultsOutput;
import org.elasticsearch.index.query.QueryBuilder;

import java.util.Optional;

/**
 * elasticsearche工具类
 *
 * @author xiongyan
 * @date 2018-06-14
 */
public class ElasticSearchUtils {

    public static void query(QueryInput input, boolean noKeyWords, Class clazz, Action action) {
        Optional<QueryResultsOutput> optional = ElasticsearchHandle.instance().search(input, noKeyWords, clazz);
        optional.ifPresent(output -> action.action(output));
    }

    public static void multipleFieldValueQuery(QueryInput input, QueryBuilder queryBuilder, Class clazz, Action action) {
        Optional<QueryResultsOutput> optional = ElasticsearchHandle.instance().searchMultipleConditions(input, queryBuilder, clazz);
        optional.ifPresent(output -> action.action(output));
    }

    public static Long total(QueryInput input, boolean noKeyWords) {
        return ElasticsearchHandle.instance().total(input, noKeyWords);
    }

    public static Long totalMultipleConditions(QueryInput input, QueryBuilder queryBuilder) {
        return ElasticsearchHandle.instance().totalMultipleConditions(input, queryBuilder);
    }

    public static boolean updateIndex(String index, String type, String id, Doc<String, Object> doc) {
        return ElasticsearchHandle.instance().updateIndex(index, type, id, doc);
    }

    public interface Action {
        /**
         * 执行操作
         *
         * @param output 参数
         */
        void action(QueryResultsOutput output);
    }
}
